home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte17 / mapvwof.c < prev    next >
C/C++ Source or Header  |  1996-01-31  |  8KB  |  260 lines

  1.  
  2. #include <windows.h>  
  3. #include "MapVwOF.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. DWORD AnotherProcess( LPDWORD lpdwParam )
  108. {
  109.    HANDLE hFileMap;    
  110.    LPTSTR lpMapAddress; 
  111.  
  112.    // Open the file mapping.
  113.    //.......................
  114.    hFileMap = OpenFileMapping( FILE_MAP_ALL_ACCESS, FALSE, 
  115.                                "MyFileMappingObject" );
  116.  
  117.    // Map a view to the mapping object.
  118.    //..................................
  119.    lpMapAddress = MapViewOfFile( hFileMap, FILE_MAP_ALL_ACCESS, 
  120.                                  0, 0, 0);
  121.  
  122.    // Display the contents of the memory.
  123.    //....................................
  124.    MessageBox( NULL, lpMapAddress, "Mapped Memory", 
  125.                MB_OK | MB_ICONINFORMATION );
  126.  
  127.    // Set "Received" into the memory.
  128.    //................................
  129.    strcpy( lpMapAddress, "Received" );
  130.  
  131.    UnmapViewOfFile( lpMapAddress );
  132.  
  133.    return( 0 );
  134. }
  135.  
  136. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  137. {
  138. static HANDLE hMapFile = NULL;
  139. static HANDLE hThread  = NULL;
  140. static LPTSTR lpMapAddress = NULL;
  141.  
  142.    switch( uMsg )
  143.    {
  144.       case WM_CREATE :
  145.               // Create a file mapped object.
  146.               //.............................
  147.               hMapFile = CreateFileMapping( (HANDLE)0xFFFFFFFF, NULL,
  148.                                             PAGE_READWRITE,
  149.                                             0,
  150.                                             1024,
  151.                                            "MyFileMappingObject" );
  152.  
  153.               // Return if the mapping failed.
  154.               //..............................
  155.               if ( !hMapFile )
  156.                  return(-1);
  157.  
  158.               break;
  159.  
  160.  
  161.       case WM_TIMER :
  162.               // Check to see if the thread has processed the memory.
  163.               //.....................................................
  164.               if ( strcmp( lpMapAddress, "Received" ) == 0 )
  165.               {
  166.                  // Kill the timer and thread.
  167.                  //...........................
  168.                  KillTimer( hWnd, 1 );
  169.                  TerminateThread( hThread, 0 );
  170.  
  171.                  // Show the mapped data and unmap the view.
  172.                  //.........................................
  173.                  MessageBox( hWnd, lpMapAddress, "Done", MB_OK );
  174.  
  175.                  UnmapViewOfFile( lpMapAddress );
  176.               }
  177.               break;
  178.  
  179.       case WM_COMMAND :
  180.               switch( LOWORD( wParam ) )
  181.               {
  182.                  case IDM_TEST :
  183.                         {
  184.                            DWORD  dwID;
  185.  
  186.                            // Map a view of the file mapped object.
  187.                            //......................................
  188.                            lpMapAddress = MapViewOfFile( hMapFile, 
  189.                                                          FILE_MAP_ALL_ACCESS, 
  190.                                                          0, 0, 0);
  191.  
  192.                            // Place data in the view.
  193.                            //........................
  194.                            strcpy( lpMapAddress, 
  195.                                    "Data passed from main process." );
  196.  
  197.                            // Make sure the data is flushed to the file.
  198.                            //...........................................
  199.                            FlushViewOfFile( lpMapAddress, 
  200.                                             lstrlen( lpMapAddress ) );
  201.  
  202.                            // Create a thread to receive the data.
  203.                            //.....................................
  204.                            hThread = CreateThread( NULL, 0, 
  205.                                       (LPTHREAD_START_ROUTINE)AnotherProcess,
  206.                                       NULL, 0, &dwID );
  207.  
  208.                            // Set a timer to wait for the data to be processed.
  209.                            //..................................................
  210.                            SetTimer( hWnd, 1, 1000, NULL );
  211.                         }
  212.                         break;
  213.  
  214.                  case IDM_ABOUT :
  215.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  216.                         break;
  217.  
  218.                  case IDM_EXIT :
  219.                         DestroyWindow( hWnd );
  220.                         break;
  221.               }
  222.               break;
  223.       
  224.       case WM_DESTROY :
  225.               CloseHandle( hMapFile );
  226.  
  227.               PostQuitMessage(0);
  228.               break;
  229.  
  230.       default :
  231.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  232.    }
  233.  
  234.    return( 0L );
  235. }
  236.  
  237.  
  238. LRESULT CALLBACK About( HWND hDlg,           
  239.                         UINT message,        
  240.                         WPARAM wParam,       
  241.                         LPARAM lParam)
  242. {
  243.    switch (message) 
  244.    {
  245.        case WM_INITDIALOG: 
  246.                return (TRUE);
  247.  
  248.        case WM_COMMAND:                              
  249.                if (   LOWORD(wParam) == IDOK         
  250.                    || LOWORD(wParam) == IDCANCEL)    
  251.                {
  252.                        EndDialog(hDlg, TRUE);        
  253.                        return (TRUE);
  254.                }
  255.                break;
  256.    }
  257.  
  258.    return (FALSE); 
  259. }
  260.